package com.example.leetdemo.finash;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Solution676 {

    public static void main(String[] args) {

    }

    static class MagicDictionary {

        private Map<Integer, Set<String>> map;

        public MagicDictionary() {
            map = new HashMap<>();
        }

        public void buildDict(String[] dictionary) {
            for (int i = 0; i < dictionary.length; i++) {
                Set<String> stringSet = map.getOrDefault(dictionary[i].length(), new HashSet<String>());
                stringSet.add(dictionary[i]);
                map.put(dictionary[i].length(), stringSet);
            }
        }

        public boolean search(String searchWord) {
            Set<String> stringSet = map.getOrDefault(searchWord.length(), new HashSet<>());
            for (String str : stringSet) {
                int size = 0;
                for (int i = 0; i < str.length(); i++) {
                    if (str.charAt(i) != searchWord.charAt(i)) {
                        size++;
                    }
                    if (size > 1) {
                        break;
                    }
                }
                if (size == 1) {
                    return true;
                }
            }
            return false;
        }
    }
}
